package org.llc.gof.structure.flyweight.basic;

import java.util.HashMap;

/**
 * @author harvey
 */
public class FlyweightFactory {

    private static HashMap<String, Flyweight> pool = new HashMap<>();

    /**
     * 享元工厂
     *
     * @param extrinsic
     * @return
     */
    public static Flyweight getFlyweight(String extrinsic) {
        //需要返回的对象
        Flyweight flyweight;
        //在池中没有改对象
        if (pool.containsKey(extrinsic)) {
            flyweight = pool.get(extrinsic);
        } else {
            //根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight1(extrinsic);
            //放置到池中
            pool.put(extrinsic, flyweight);
        }
        return flyweight;
    }

}
